package com.hjb.service.controller.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hjb.service.cms.CmsLinkService;
import com.hjb.service.common.BannerService;
import com.hjb.service.config.constant.Constants;
import com.hjb.service.config.properties.WxProperties;
import com.hjb.service.domain.cmslink.CmsLink;
import com.hjb.service.domain.cmslink.CmsLinkExample;
import com.hjb.service.utils.MapUtil;

/**
 * Banner Controller
 * @author zhangxl
 * @date 2017-07-20
 * @version 1.0
 */
@RestController
@RequestMapping("/common")	
public class BannerController {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private CmsLinkService cmsLinkService;

	@Autowired
	private WxProperties wxProperties;
	
	/**
	 * banner列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/banner/list")
	@ResponseBody
	public Map<String, Object> bannerList(HttpServletRequest request){
		/*Map<String,Object> dataMap = new HashMap<String, Object>();
		Map<String,Object> resMap =MapUtil.getRetMap();
		try {
			
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("channel", Constants.BANNER_CHANNEL_APP);
			//公告列表
			List<Map<String,Object>> bannelList = bannerService.getBannerList(params);
			
			dataMap.put("bannelList", bannelList);
			resMap.put("data", dataMap);
			
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");	
		}
		
		return resMap;*/
		Map<String, Object> result = MapUtil.getRetMap();
		Map<String, Object> data = new HashMap<String, Object>();
		result.put("data", data);
		
		List<Object> bannerList = new ArrayList<Object>();
		CmsLinkExample cmsLinkExample = new CmsLinkExample();
		cmsLinkExample.createCriteria().andDelFlagEqualTo("0").andRemarksEqualTo(Constants.BANNER_CHANNEL_APP)/*.andCategoryIdEqualTo("1")*/;
		cmsLinkExample.setOrderByClause("weight desc,update_date desc");
		List<CmsLink> cmsLinkList = cmsLinkService.selectByExample(cmsLinkExample);
		if (cmsLinkList != null && cmsLinkList.size() > 0) {
			for (CmsLink cmsLink : cmsLinkList) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("bannerName", cmsLink.getTitle());
				map.put("linkUrl", cmsLink.getHref());
//				map.put("iconUrl", wxProperties.getImg_url() + cmsLink.getImage());
				map.put("iconUrl", cmsLink.getImage());
				bannerList.add(map);
			}
		}
		data = ((Map<String, Object>) result.get("data"));
		data.put("bannerList", bannerList);
		return result;
	}
	
}
